package cn.lg.soar.system.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.auth.entity.DataRule;
import cn.lg.soar.system.auth.service.IDataRuleService;
import io.swagger.annotations.Api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = "lg.database.enabled-data-permission", havingValue = "true", matchIfMissing = true)
@Validated
@Api(tags = "数据规则管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/data-rule")
public class DataRuleController extends BaseController<DataRule, IDataRuleService> {

    @Override
    protected Serializable getId(DataRule entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "数据规则";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated DataRule data) {
        return createResult(data);
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated DataRule data) {
        return updateResult(data);
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DetailMapping
    public HttpResult<DataRule> detail(@Id Long id) {
        return detailResult(id);
    }

    @QueryMapping
    public HttpResult<PageResult<DataRule>> query(PageQuery pageQuery, QueryParams params){
       return queryResult(pageQuery, params);
    }

    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

}
